package org.soarsky.wealthchengdu.module.comment;

import java.util.ArrayList;
import java.util.List;

import org.soarsky.wealthchengdu.R;
import org.soarsky.wealthchengdu.appliction.MyAppliction;
import org.soarsky.wealthchengdu.entity.CommentData;
import org.soarsky.wealthchengdu.entity.CommentResultMsg;
import org.soarsky.wealthchengdu.entity.JsonResult;
import org.soarsky.wealthchengdu.entity.LogonResult;
import org.soarsky.wealthchengdu.language.LanguageUtil;
import org.soarsky.wealthchengdu.network.ParserResults;
import org.soarsky.wealthchengdu.util.Config;
import org.soarsky.wealthchengdu.view.ALLSendDialog;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class CommentActivity extends Activity {
	TextView title_tv_text;// title 标题
	ImageView title_iv_img;// title 左返回按钮
	ListView com_listview;// 下方ListView
	CommentAdapter mAdapter;
	private LanguageUtil util;
	private String titleName;
	private EditText commentEd;
	Button Select;
	EditText selectEdiText;
	private CommentResultMsg commentResultMsg;
	private List<CommentData> listdata;
	
	private static ParserResults service;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_comment);
		service = new ParserResults();
		listdata = new ArrayList<CommentData>();
		intnview();

		// data2 = getResources().getStringArray(R.array.com_adapter_text);
		title_tv_text.setText(titleName);
		commentEd.setHint(titleName+"...");
		title_iv_img.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				CommentActivity.this.finish();
			}
		});
		Select.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String text=selectEdiText.getText().toString();
				loadDatas(text);
			}
		});
		mAdapter = new CommentAdapter(CommentActivity.this);
		com_listview.setAdapter(mAdapter);
	}

	// 初始化控件
	public void intnview() {
		selectEdiText=(EditText)findViewById(R.id.comment_ed);
		Select=(Button)findViewById(R.id.select);
		ALLSendDialog.showdialog(CommentActivity.this);
		util = Config.changeLanguage(MyAppliction.mCn);
		titleName = util.getComment();
		title_tv_text = (TextView) findViewById(R.id.title_tv_text);
		title_iv_img = (ImageView) findViewById(R.id.title_iv_img);
		com_listview = (ListView) findViewById(R.id.com_listview);
		commentEd=(EditText)findViewById(R.id.comment_ed);
		loadData();
	}

	
	private void loadDatas(final String s) {

		new AsyncTask<Void, Void, Boolean>() {

			@Override
			protected Boolean doInBackground(Void... params) {
				// TODO Auto-generated method stub
				getLogon();
				try {
//					JsonResult<CommentResultMsg> result = service
//							.getComment(MyAppliction.ID, "5", "1");
					Log.e("ss", MyAppliction.ID+"@@@@@@@@@@@@@@@@@@@@@@@@@@");
					JsonResult<String> result=service.sendComment(MyAppliction.ID, s);
					if (result == null)
						return false;
					if (result.isError()) {
						error(result);
						// 这里做 数据错误处理
						return false;
					} 
				} catch (Exception e) {
					// TODO Auto-generated catch block
				}
				return false;
			}

			protected void onPostExecute(Boolean result) {
				// UI更新操作
				if (result) {
					mAdapter.setlistdata(listdata);
				} else {
                     Toast.makeText(CommentActivity.this, "没有数据....", Toast.LENGTH_LONG).show();
				}
				ALLSendDialog.dimssdialog();
			};
		}.execute();
	}
	
	
	
	
	
	
	
	
	
	
	private void loadData() {

		new AsyncTask<Void, Void, Boolean>() {

			@Override
			protected Boolean doInBackground(Void... params) {
				// TODO Auto-generated method stub
				getLogon();
				try {
					JsonResult<CommentResultMsg> result = service
							.getComment(MyAppliction.ID, "5", "1");
					if (result == null)
						return false;
					if (result.isError()) {
						error(result);
						// 这里做 数据错误处理
						return false;
					} else {
						CommentResultMsg dataMsg = result.getData();
						List<CommentData> list = dataMsg.getData();
						if(list!=null&&list.size()!=0){//获取数据
							for(CommentData d:list){
								listdata.add(d);
							}
							return true;
						}

					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
				}
				return false;
			}

			protected void onPostExecute(Boolean result) {
				// UI更新操作
				if (result) {
					mAdapter.setlistdata(listdata);
				} else {
                     Toast.makeText(CommentActivity.this, "没有数据....", Toast.LENGTH_LONG).show();
				}
				ALLSendDialog.dimssdialog();
			};
		}.execute();
	}

	// 必须先登录
	private void getLogon() {
		try {
			JsonResult<LogonResult> jsonResult = service.authLogon("张三");
			if (jsonResult != null) {
				if (jsonResult.isError()) {
					error(jsonResult);
				} else {// 登录成功
					System.out.println(jsonResult.getData().getUserId() + "==="
							+ jsonResult.getData().getNickName());
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println(e.toString() + "===");
		}
	}

	private static void error(JsonResult<?> jsonResult) {
		System.out.println("错误码:" + jsonResult.getCode() + "===错误信息:"
				+ jsonResult.getMsg());
	}
}
